# 导入必要的库
import pygame
import sys

# 定义游戏参数
WINDOW_WIDTH = 640  # 窗口宽度
WINDOW_HEIGHT = 640  # 窗口高度
GRID_SIZE = 80  # 棋盘格子的大小
BOARD_COLOR_1 = (238, 238, 210)  # 棋盘浅色
BOARD_COLOR_2 = (118, 150, 86)  # 棋盘深色

# 初始化 Pygame
pygame.init()
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('象棋游戏')
# 绘制棋盘




# # 开始游戏循环
# game_loop()
# 棋子类
class ChessPiece:
    def __init__(self, row, col, color):
        self.row = row
        self.col = col
        self.color = color

    def move(self, new_row, new_col):
        self.row = new_row
        self.col = new_col

# 初始化棋子
red_pieces = [ChessPiece(0, 0, 'red'), ChessPiece(0, 1, 'red')]  # 红方棋子
black_pieces = [ChessPiece(9, 0, 'black'), ChessPiece(9, 1, 'black')]  # 黑方棋子

# 绘制棋子
def draw_pieces(screen):
    for piece in red_pieces + black_pieces:
        pygame.draw.circle(
            screen,
            (255, 0, 0) if piece.color == 'red' else (0, 0, 0),
            (piece.col * GRID_SIZE + GRID_SIZE // 2, piece.row * GRID_SIZE + GRID_SIZE // 2),
            GRID_SIZE // 3
        )

# 移动棋子
def move_piece(piece, new_row, new_col):
    piece.move(new_row, new_col)

# 判断是否合法移动
def is_valid_move(piece, new_row, new_col):
    # 在这里实现棋子的合法移动规则
    return True  # 暂时返回 True，表示任何移动都合法

# 处理棋子移动
def handle_piece_movement(piece, new_row, new_col):
    if is_valid_move(piece, new_row, new_col):
        move_piece(piece, new_row, new_col)

# 主游戏循环
def game_loop():
    running = True
    selected_piece = None
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                row = event.pos[1] // GRID_SIZE
                col = event.pos[0] // GRID_SIZE
                for piece in red_pieces + black_pieces:
                    if piece.row == row and piece.col == col:
                        selected_piece = piece
            elif event.type == pygame.MOUSEBUTTONUP:
                if selected_piece:
                    new_row = event.pos[1] // GRID_SIZE
                    new_col = event.pos[0] // GRID_SIZE
                    handle_piece_movement(selected_piece, new_row, new_col)
                    selected_piece = None
        draw_board(screen)  # 每次循环都绘制棋盘
        draw_pieces(screen)  # 绘制棋子
        pygame.display.flip()  # 刷新屏幕内容
    pygame.quit()
def draw_board(screen):
    for row in range(8):
        for col in range(8):
            color = BOARD_COLOR_1 if (row + col) % 2 == 0 else BOARD_COLOR_2
            pygame.draw.rect(
                screen,
                color,
                pygame.Rect(col * GRID_SIZE, row * GRID_SIZE, GRID_SIZE, GRID_SIZE)
            )

# # 主游戏循环
# def game_loop():
#     running = True
#     while running:
#         for event in pygame.event.get():
#             if event.type == pygame.QUIT:
#                 running = False
#         draw_board(screen)  # 每次循环都绘制棋盘
#         pygame.display.flip()  # 刷新屏幕内容
#     pygame.quit()
    # 在 game_loop() 函数中添加以下内容：

# 处理鼠标点击事件
def handle_mouse_click(selected_piece, event):
    row = event.pos[1] // GRID_SIZE
    col = event.pos[0] // GRID_SIZE
    for piece in red_pieces + black_pieces:
        if piece.row == row and piece.col == col:
            selected_piece = piece
    return selected_piece

# 处理鼠标释放事件
def handle_mouse_release(selected_piece, event):
    if selected_piece:
        new_row = event.pos[1] // GRID_SIZE
        new_col = event.pos[0] // GRID_SIZE
        handle_piece_movement(selected_piece, new_row, new_col)
        selected_piece = None
    return selected_piece

# 主游戏循环
# def game_loop():
#     running = True
#     selected_piece = None
#     while running:
#         for event in pygame.event.get():
#             if event.type == pygame.QUIT:
#                 running = False
#             elif event.type == pygame.MOUSEBUTTONDOWN:
#                 selected_piece = handle_mouse_click(selected_piece, event)
#             elif event.type == pygame.MOUSEBUTTONUP:
#                 selected_piece = handle_mouse_release(selected_piece, event)
#         draw_board(screen)  # 每次循环都绘制棋盘
#         draw_pieces(screen)  # 绘制棋子
#         pygame.display.flip()  # 刷新屏幕内容
#     pygame.quit()
# 在 game_loop() 函数中添加以下内容：

# 判断游戏结束条件
def is_game_over():
    # 在这里实现判断游戏结束的逻辑
    return False  # 暂时返回 False，表示游戏不会结束

# 主游戏循环
def game_loop():
    running = True
    selected_piece = None
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                selected_piece = handle_mouse_click(selected_piece, event)
            elif event.type == pygame.MOUSEBUTTONUP:
                selected_piece = handle_mouse_release(selected_piece, event)
        draw_board(screen)  # 每次循环都绘制棋盘
        draw_pieces(screen)  # 绘制棋子
        pygame.display.flip()  # 刷新屏幕内容
        if is_game_over():
            running = False
    pygame.quit()

# 开始游戏循环
game_loop()
